package com.gla.service;

import com.gla.dao.SearchMapper;
import com.gla.domain.Commodity;
import com.gla.domain.Shop;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class SearchService {
    @Resource
    SearchMapper searchMapper;

    public List<Shop> selectNearShop(Double la,Double lo){
        List<Shop> shopList = searchMapper.selectAllAhop();
        List<Shop> nearShopList = new ArrayList<>();
        for(Shop shop : shopList){
            Double shopLa = (double)shop.getLatitude();
            Double shopLo = (double)shop.getLongitude();
            //根据经纬度算距离
            int distant = (int) (Math.sqrt((lo - shopLo) * (lo - shopLo) + (la - shopLa) * (la - shopLa)) * 100000);
            //附近商店标准
            if(distant <= 5000){
                nearShopList.add(shop);
            }
        }
        return nearShopList;
    }

    public List<Shop> searchShop(String keyword){
        List<Shop> searchShop = searchMapper.searchShop(keyword);
        System.out.println(searchShop);
        return searchShop;
    }

    public List<Commodity> searchPet(String keyword){
        List<Commodity> searchPet = searchMapper.searchPet(keyword);
        return searchPet;
    }

    public List<Commodity> searchPetGoods(String keyword){
        List<Commodity> searchPetGoods = searchMapper.searchPetGoods(keyword);
        return searchPetGoods;
    }

    public List<Commodity> getSearchFine(Integer cat,Integer dog,Integer chuxing,Integer weisheng,Integer wanju,Integer shiping){
        List<Commodity> searchFine = new ArrayList<>();
        if(cat>0){
            List<Commodity> commodities = searchMapper.getSearchFine(3);
            if(commodities.size()>0){
                Random random = new Random ();
                boolean[]  bool = new boolean[25];
                int randInt = 0;
                if(cat>commodities.size()) cat = commodities.size(); //保证不会陷入死循环
                for(int j = 0; j < cat ; j++) {
                    /**得到不同的随机数*/
                    do{
                        randInt  = random.nextInt(commodities.size()); //随机获取数组下标
                    }while(bool[randInt]);
                    bool[randInt] = true;
                    searchFine.add(commodities.get(randInt)); //添加金属组
                }
            }
            System.out.println("cat  "+searchFine);
        }

        if(dog>0){
            List<Commodity> commodities = searchMapper.getSearchFine(1);
            if(commodities.size()>0){
                Random random = new Random ();
                boolean[]  bool = new boolean[25];
                int randInt = 0;
                if(dog>commodities.size()) dog = commodities.size(); //保证不会陷入死循环
                for(int j = 0; j < dog ; j++) {
                    /**得到不同的随机数*/
                    do{
                        randInt  = random.nextInt(commodities.size()); //随机获取数组下标
                    }while(bool[randInt]);
                    bool[randInt] = true;
                    searchFine.add(commodities.get(randInt)); //添加金属组
                }
            }
        }
        if(chuxing>0){
            List<Commodity> commodities = searchMapper.getSearchFine(6);
            if(commodities.size()>0){
                Random random = new Random ();
                boolean[]  bool = new boolean[25];
                int randInt = 0;
                if(chuxing>commodities.size()) chuxing = commodities.size(); //保证不会陷入死循环
                for(int j = 0; j < chuxing ; j++) {
                    /**得到不同的随机数*/
                    do{
                        randInt  = random.nextInt(commodities.size()); //随机获取数组下标
                    }while(bool[randInt]);
                    bool[randInt] = true;
                    searchFine.add(commodities.get(randInt)); //添加金属组
                }
            }
        }
        if(weisheng>0){
            List<Commodity> commodities = searchMapper.getSearchFine(2);
            if(commodities.size()>0){
                Random random = new Random ();
                boolean[]  bool = new boolean[25];
                int randInt = 0;
                if(weisheng>commodities.size()) weisheng = commodities.size(); //保证不会陷入死循环
                for(int j = 0; j < weisheng ; j++) {
                    /**得到不同的随机数*/
                    do{
                        randInt  = random.nextInt(commodities.size()); //随机获取数组下标
                    }while(bool[randInt]);
                    bool[randInt] = true;
                    searchFine.add(commodities.get(randInt)); //添加金属组
                }
            }
        }
        if(wanju>0){
            List<Commodity> commodities = searchMapper.getSearchFine(5);
            if(commodities.size()>0){
                Random random = new Random ();
                boolean[]  bool = new boolean[25];
                int randInt = 0;
                if(wanju>commodities.size()) wanju = commodities.size(); //保证不会陷入死循环
                for(int j = 0; j < wanju ; j++) {
                    /**得到不同的随机数*/
                    do{
                        randInt  = random.nextInt(commodities.size()); //随机获取数组下标
                    }while(bool[randInt]);
                    bool[randInt] = true;
                    searchFine.add(commodities.get(randInt)); //添加金属组
                }
            }
        }
        if(shiping>0){
            List<Commodity> commodities = searchMapper.getSearchFine(4);
            if(commodities.size()>0){
                Random random = new Random ();
                boolean[]  bool = new boolean[25];
                int randInt = 0;
                if(shiping>commodities.size()) shiping = commodities.size(); //保证不会陷入死循环
                for(int j = 0; j < shiping ; j++) {
                    /**得到不同的随机数*/
                    do{
                        randInt  = random.nextInt(commodities.size()); //随机获取数组下标
                    }while(bool[randInt]);
                    bool[randInt] = true;
                    searchFine.add(commodities.get(randInt)); //添加金属组
                }
            }

        }
        return searchFine;
    }
}
